<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-6-3
 * Func   统计
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('user_money_log');
$rtable = Table('user_money_recharge');
$ctable = Table('user_money_cash');
$utable = Table('user');

$dt1 = NoBadStr($_GET['dt1']);
!$dt1 && $dt1 = date('Y-m-d',strtotime('-1 month'));
$dt2 = NoBadStr($_GET['dt2']);
!$dt2 && $dt2 = date('Y-m-d');

$where = ' AND 1 ';

$title = NoBadStr($_GET['title']);
$title && $where .= ' AND u.username LIKE \''.$title.'\' ';
	
$tmp1 = strtotime($dt1);
$tmp2 = strtotime($dt2);
$where .= ' AND t.dateline >= '.$tmp1.' AND t.dateline <= '.$tmp2.' GROUP BY FROM_UNIXTIME(t.dateline,\'%Y%m%d\') ';

if($a == 'lottery')
{
	//	每日投注/中奖
	$charts = ['invest'=>[],'prize'=>[]];
	
	//	彩种
	$lotterys = $db->getall('SELECT id,title,credit FROM '.Table('lottery').' ORDER BY id ','id');
	
	$cid = intval($_GET['cid']);
	if(!$cid)
	{
		$tmp = $lotterys;
		$tmp = array_shift($tmp);
		
		$cid = $tmp['id'];
	}
	
	$where = str_replace(' AND 1 ',' AND t.cid = '.$cid,$where);
	
	$t1 = $t2 = [];
	$tmp = $db->getall('SELECT SUM(t.money) AS p,FROM_UNIXTIME(t.dateline,\'%Y%m%d\') AS dt FROM '.Table('lottery_order_list').' t LEFT JOIN '.$utable.' u ON t.uid = u.id WHERE t.status <> 1 '.$where);
	foreach($tmp as $val)
	{
		$t1[$val['dt']] = $val['p'];
	}
	$tmp = $db->getall('SELECT SUM(t.money) AS p,FROM_UNIXTIME(t.dateline,\'%Y%m%d\') AS dt FROM '.Table('lottery_order_list').' t LEFT JOIN '.$utable.' u ON t.uid = u.id WHERE t.prize > 0 '.$where);
	foreach($tmp as $val)
	{
		$t2[$val['dt']] = $val['p'];
	}

	for($i = $tmp1; $i < $tmp2;)
	{
		$charts['invest'][date('md',$i)] = intval($t1[date('Ymd',$i)]);
		$charts['prize'][date('md',$i)] = intval($t2[date('Ymd',$i)]);
		
		$i += 86400;
	}
	
	$investtotal = array_sum($charts['invest']);
	$prizetotal = array_sum($charts['prize']);
	
	$tpl->display('user/count_lottery');
}
else if($a == 'recharge')
{
    //	每日充值/提现
	$charts = ['recharge'=>[],'cash'=>[]];
	
	$t1 = $t2 = [];
	$tmp = $db->getall('SELECT SUM(t.money) AS p,FROM_UNIXTIME(t.dateline,\'%Y%m%d\') AS dt FROM '.$rtable.' t LEFT JOIN '.$utable.' u ON t.uid = u.id WHERE t.status = 1 '.$where);
	foreach($tmp as $val)
	{
		$t1[$val['dt']] = $val['p'];
	}
	$tmp = $db->getall('SELECT SUM(t.money) AS p,FROM_UNIXTIME(t.dateline,\'%Y%m%d\') AS dt FROM '.$ctable.' t LEFT JOIN '.$utable.' u ON t.uid = u.id WHERE t.status = 1 '.$where);
	foreach($tmp as $val)
	{
		$t2[$val['dt']] = $val['p'];
	}

	for($i = $tmp1; $i < $tmp2;)
	{
		$charts['recharge'][date('md',$i)] = intval($t1[date('Ymd',$i)]);
		$charts['cash'][date('md',$i)] = intval($t2[date('Ymd',$i)]);
		
		$i += 86400;
	}
	
	$rechargetotal = array_sum($charts['recharge']);
	$cashtotal = array_sum($charts['cash']);
	
	$tpl->display('user/count_recharge');
}
else
{
	//	每日投注/中奖
	$charts = ['invest'=>[],'prize'=>[]];
	
	$t1 = $t2 = [];
	$tmp = $db->getall('SELECT SUM(t.money) AS p,FROM_UNIXTIME(t.dateline,\'%Y%m%d\') AS dt FROM '.$table.' t LEFT JOIN '.$utable.' u ON t.uid = u.id WHERE t.tid = \'order_add\' '.$where);
	foreach($tmp as $val)
	{
		$t1[$val['dt']] = $val['p'];
	}
	$tmp = $db->getall('SELECT SUM(t.money) AS p,FROM_UNIXTIME(t.dateline,\'%Y%m%d\') AS dt FROM '.$table.' t LEFT JOIN '.$utable.' u ON t.uid = u.id WHERE t.tid = \'prize\' '.$where);
	foreach($tmp as $val)
	{
		$t2[$val['dt']] = $val['p'];
	}

	for($i = $tmp1; $i < $tmp2;)
	{
		$charts['invest'][date('md',$i)] = intval($t1[date('Ymd',$i)]);
		$charts['prize'][date('md',$i)] = intval($t2[date('Ymd',$i)]);
		
		$i += 86400;
	}
	
	$investtotal = array_sum($charts['invest']);
	$prizetotal = array_sum($charts['prize']);
	
	$tpl->display('user/count');
}